﻿using ACC.Business.Stn;
using ACC.Business;
using ACC.Data;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using System;
using ACC.Device.PLC.Siemens;
using ACC.Interface.Extensions;
using System.Text;
using System.Data;
using ACC.Data.ACCLog;
using ACC.Util.Convert;

namespace Script
{
    /// <summary>
    /// 工装夹紧
    /// OP L1M03070 STATION L1M13071, L1M23071
    /// 写入Pack总成型号和订单数量
    /// </summary>
    class L1M03070 : StnProcessPL02_JT
    {
        public L1M03070(StnRunningData data)
            : base(data)
        {
            IsReport = true;
            IsPalletRelationSN = false;
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                switch (_data.StnCfg.Name)
                {
                    case "L1M13071":
                        {
                            kukaSiemensDev.DB190_Offset = 2000;
                        }
                        break;
                    case "L1M23071":
                        {
                            kukaSiemensDev.DB190_Offset = 1000;
                        }
                        break;
                }

                kukaSiemensDev.DB190 = 800;
                //kukaSiemensDev.DB190_Offset = 2000;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 66); // TO-DO: 66 ?
                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 2);  // (ushort)(kukaSiemensDev.DB190_Offset + 34);
                kukaSiemensDev.DB193_ACC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 68);
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 2);  // (ushort)(kukaSiemensDev.DB190_Offset + 34);
                kukaSiemensDev.DB194_PLC_Data_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 78);

                kukaSiemensDev.ProcessDataStringDataLength = 8;
                kukaSiemensDev.UnloadBarcodeCount = 2;
                //kukaSiemensDev.ProcessDataStringLength = 2;
                //kukaSiemensDev.ProcessDataRealLength = 5;
            }

            return dev;
        }

        public override ACCCode ReadLoadParam(ACCDevice dev, out ACCDevice.LoadParam param)
        {
            ACCCode rs = base.ReadLoadParam(dev, out param);
            // 读取到模组基体号和托盘号
            PalletNo = param.ComponentSN.Length > 0 ? param.ComponentSN[0] : string.Empty;
            param.ComponentSN = new string[] { };
            try
            {
                string partNo, orderNo;
                int planCnt;
                string wono = GetWorkOrder(_lineData.Line, string.Empty, out partNo, out orderNo, out planCnt);
                if (!string.IsNullOrEmpty(wono) && !string.IsNullOrEmpty(orderNo))
                {
                    // 子线（基体，模组，上线体）的OrderNo（订单号）是主线（PACK）的OrderNo 加上下划线"_"加F001/2/3/4/5...
                    GetWorkOrderByOrderNo("PL02", orderNo.Split('_')[0], out partNo);

                    // Pack 的型号发给PLC
                    rs = SendBarcodeToPlc(new string[] { partNo }, kukaSiemensDev.DB190_Offset + 586, 12);
                    // 工单计划数量发给PLC
                    rs = SendInt16ToPlc(new int[] { planCnt }, kukaSiemensDev.DB190_Offset + 598);

                    StnEventLog.Log(_data, StnLog.LogType.Event,
                        string.Format("WoNo = {0}, OrderNo = {1}, Pack PartNo = {2}", wono, orderNo, partNo));
                }
                else
                {
                    // 没有工单需要生产
                    return ACCCode.UserDef_100;

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message + "\r\n" + ex.StackTrace);
            }
            return rs;
        }

        public override ACCCode ReadUnloadParam(ACCDevice dev, out ACCDevice.UnloadParam param)
        {
            ACCCode rs = base.ReadUnloadParam(dev, out param);
            // 已经读到模组基体号和托盘号
            //param.UnitSN = _data.LastLoadUnit.UnitSN;
            PalletNo = param.ComponentSN.Length > 0 ? param.ComponentSN[0] : string.Empty;
            param.ComponentSN = new string[] { };

            return rs;
        }

        /// <summary>
        ///将托盘号和模组基体码的对应关系存入[ACC_PALLETS]中，在PL02_MZ线贴加热膜站时根据托盘号查询模组基体码和模组内部码绑定
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override ACCCode SendUnloadResult(ACCDevice dev, ACCDevice.UnloadResult result)
        {
            ACCCode rs = base.SendUnloadResult(dev, result);

            if (rs == ACCCode.ACCOK && result.ErrorCode == ACCCode.ACCOK)
            {
                if (!string.IsNullOrEmpty(PalletNo))
                {
                    //this.AddPallet(PalletNo, _data.LastUnloadUnit.UnitSN, string.Empty, string.Empty);
                    this.AddPallet(PalletNo, _data.LastUnloadUnit.UnitSN, string.Empty, string.Empty, "PL02_MZ");
                }
                else
                {
                    StnEventLog.Log(_data, StnLog.LogType.Error,
                        string.Format("托盘号关联基体码[{0}]失败，托盘号为空", _data.LastUnloadUnit.UnitSN));
                }
            }

            return rs;
        }
    }
}

